package LeetCode.TwoPointers;

import java.util.Arrays;

/**
 * @author : LdLtd
 * @Date : 2023/7/23
 * @Description:283. 移动零
 */
public class move_zeroes {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] ints = {0,0,1};
        solution.moveZeroes(ints);
        System.out.println(Arrays.toString(ints));
    }
   static class Solution {
        /*比较慢*/
        public void moveZeroes(int[] nums) {
            int len = nums.length;
            int l=0,r=len-1;
            while(l<=r){
                if(nums[l]==0){
                    for (int i = l; i < r; i++) {
                        nums[i]=nums[i+1];
                    }
                    nums[r--]=0;
                }else{
                    l++;
                }
            }
        }
       public void moveZeroes1(int[] nums) {
            if(nums==null) return;

            //第一次遍历记录非0的个数，最后j指向最后一个不为0的数,并且往前移动
           int j=0;
           for (int i = 0; i < nums.length; i++) {
               if(nums[i]!=0){
                   nums[j++]=nums[i];
               }
           }
           //第二次后面的赋值为0
           Arrays.fill(nums,j,nums.length,0);
       }
    }
}
